package com.dindang.maintenance.mvp.presenter;

import android.app.Application;

import com.dindang.maintenance.R;
import com.dindang.maintenance.mvp.contract.PlanContract;
import com.dindang.maintenance.mvp.model.entity.BaseResponse;
import com.dindang.maintenance.mvp.model.entity.Repair;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.longxi.gamer_base.BaseAppConst;
import com.longxi.gamer_base.MyApplication;
import com.longxi.gamer_base.dialog.WaitDialog;
import com.longxi.gamer_base.model.MaintainDetailsEvent;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class PlanPresenter extends BasePresenter<PlanContract.Model, PlanContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private WaitDialog mWaitDialog;
    private Repair.AddMaintainPlanParam mAddMaintainPlanParam = new Repair.AddMaintainPlanParam();
    private MaintainDetailsEvent mMaintainDetailsEvent = new MaintainDetailsEvent();

    @Inject
    public PlanPresenter(PlanContract.Model model, PlanContract.View rootView) {
        super(model, rootView);
    }

    public void init() {
        mWaitDialog = new WaitDialog(mRootView.getActivity());
        initListener();
    }

    private void initListener() {
        mRootView.getTvCommit().setOnClickListener(v -> {
            mAddMaintainPlanParam.lId = mAppManager.getCurrentActivity().getIntent().getStringExtra("id");
            mAddMaintainPlanParam.cRemark = mRootView.getEtCompletionValue().getText().toString().trim();
            mAddMaintainPlanParam.fCost = mRootView.getEtPriceValue().getText().toString().trim();
            if("".equals(mAddMaintainPlanParam.cRemark)){
                ArmsUtils.makeText(mApplication, ArmsUtils.getString(mApplication, R.string.maintenance_please_enter_maintenance_plan));
                return;
            }
            if("".equals(mAddMaintainPlanParam.fCost)){
                ArmsUtils.makeText(mApplication, ArmsUtils.getString(mApplication, R.string.maintenance_please_enter_maintenance_cost));
                return;
            }
            addMaintainPlan();
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        this.mWaitDialog = null;
        this.mAddMaintainPlanParam = null;
        this.mMaintainDetailsEvent = null;
    }

    /**
     * 添加维修计划
     */
    private void addMaintainPlan(){
        mWaitDialog.showWaitingDialog(ArmsUtils.getString(mApplication, R.string.data_load));
        mModel.addMaintainPlan(mAddMaintainPlanParam)
        .subscribeOn(Schedulers.io())
        .retryWhen(new RetryWithDelay(0, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
        .doOnSubscribe(disposable -> {

        }).subscribeOn(AndroidSchedulers.mainThread())
        .observeOn(AndroidSchedulers.mainThread())
        .doFinally(() -> {
            mWaitDialog.hideWaitingDialog();
        })
        .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
        .subscribe(new ErrorHandleSubscriber<BaseResponse>(mErrorHandler) {
            @Override
            public void onNext(BaseResponse response) {
                if(response.getStatus().equals(BaseAppConst.OUT_TOKEN)){
                    MyApplication.getInstance().gotoLoginActivity();
                }
                ArmsUtils.makeText(mApplication, response.getStatuMsg());
                if(!response.isSuccess()){
                    return;
                }
                EventBus.getDefault().post(mMaintainDetailsEvent);
                mAppManager.getCurrentActivity().finish();
            }
        });
    }
}
